package com.zhs.gccs.adapter;

import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import com.zhs.gccs.listener.OnItemClickListener;
import com.zhs.gccs.listener.OnItemLongClickListener;

import java.util.List;


public abstract class MenuAdapter<T> extends RecyclerView.Adapter<MenuAdapter.VH> {

    private List<T> dataList;

    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public MenuAdapter(List<T> dataList) {
        this.dataList = dataList;
    }

    public void refreshData(List<T> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void loadMoreData(List<T> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public abstract int getLayoutId(int viewType);

    public abstract void convert(VH holder, T data, int position);

    @Override
    public void onBindViewHolder(final VH holder, int position) {
        holder.setOnItemClickListener(mOnItemClickListener);
        holder.setOnItemLongClickListener(onItemLongClickListener);
        convert(holder, dataList.get(position), position);
    }

    @Override
    public int getItemCount() {
        if (dataList != null) {
            return dataList.size();
        }
        return 0;
    }

    protected static class VH extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        private View mConvertView;
        private OnItemClickListener mOnItemClickListener;
        private OnItemLongClickListener onItemLongClickListener;
        public CountDownTimer countDownTimer;

        public VH(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
            mConvertView = itemView;
        }

        public <T extends View> T getView(int id) {
            return (T) mConvertView.findViewById(id);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
            this.onItemLongClickListener = onItemLongClickListener;
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition(), v);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (onItemLongClickListener != null) {
                onItemLongClickListener.onItemLongClick(getAdapterPosition(), v);
            }
            return false;
        }
    }


    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View realContentView = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
        return new VH(realContentView);
    }

}
